﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Company_OfficeMain : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            this.ShowMess();
            this.BindDataToLanguageDropDown();
            this.BindDataToRpOffice(1);
        }
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();

        // Bind Data To Language
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }

    #region  Message

    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage];
        btnAddNew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage];
        
    }
 
    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }
    #endregion

    #region Grid Processing
    private void BindDataToRpOffice(int CurrentPageIndex)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;

        ContactOffice obj = new ContactOffice();
        DataTable dtb = new DataTable();
        dtb = obj.Search();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage] + dtb.Rows.Count + MultipleLanguage.LBL_FOUND[nDefaultLanguage];
        RpOffice.DataSource = dtb;
        RpOffice.DataBind();
    }

    protected void RpOffice_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        ContactOffice obj = new ContactOffice();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = obj.DeleteById(nID);
                //if (nDelete > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLang]);
                //else
                //    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLang]);
                BindDataToRpOffice(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;


        }
    }
    protected void RpOffice_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblOffice = (Label)e.Item.FindControl("lblOffice");
            Label lblAddress = (Label)e.Item.FindControl("lblAddress");
            Label lblTel = (Label)e.Item.FindControl("lblTel");
            Label lblFax = (Label)e.Item.FindControl("lblFax");
            Label lblEmail = (Label)e.Item.FindControl("lblEmail");


            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            switch (nCurrentLang)
            {
                case 1:
                lblOffice.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Name1"));
                lblAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Address1"));
                lblTel.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Tel1"));
                break;

                case 2:
                lblOffice.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Name2"));
                lblAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Address2"));
                lblTel.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Tel2"));
                break;

                case 3:
                lblOffice.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Name3"));
                lblAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Address3"));
                lblTel.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Tel3"));
                break;
            }

            lblEmail.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Email"));
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ContactOfficeID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_CATEGORY_CONFIRM[nDefaultLang]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ContactOfficeID"));
        }
    }   
    #endregion

    #region Click Events
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY_ADD;
        Response.Redirect(sAdd);
    }


    #endregion


    protected void ddlLanguage_SelectedIndexChanged1(object sender, EventArgs e)
    {
       
        BindDataToRpOffice(1);
    }
}